package cn.yiyuan.auth.controller;

import cn.yiyuan.auth.domain.Permission;
import cn.yiyuan.auth.service.PermissionService;
import cn.yiyuan.base.until.AjaxResult;
import cn.yiyuan.base.until.PageInfo;
import cn.yiyuan.org.query.DepQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @user m
 * @project system
 * @date 2023-10-28
 * @package cn.yiyuan.auth.controller
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    public PermissionService permissionService;

//新增&修改
    @PutMapping
    public AjaxResult insertOrUpdate(@RequestBody Permission permission){
    try {
        if (Objects.isNull(permission.getId())){
            permissionService.insert(permission);
        }else {
            permissionService.update(permission);
        }
        return AjaxResult.success();
    } catch (Exception e) {
        e.printStackTrace();
        return AjaxResult.error();
    }
}

    //查询分页
    @PostMapping
    public AjaxResult json(@RequestBody DepQuery query){
        try {
            PageInfo<Permission> pageList = permissionService.queryPage(query);
            System.out.println(pageList);
            return AjaxResult.success(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败"+e.getMessage());
        }
    }

//获取所有数据
    @GetMapping
    public AjaxResult getAll(){
        try {
            List<Permission> list = permissionService.queryAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询失败"+e.getMessage());
        }
    }

}
